package com.stylefeng.guns.modular.info.controller;

import com.stylefeng.guns.core.base.controller.BaseController;

import com.stylefeng.guns.modular.manage.service.ICarMaService;
import com.stylefeng.guns.modular.system.model.CarInfo;
import com.stylefeng.guns.modular.system.model.CarMa;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;

import com.stylefeng.guns.modular.info.service.ICarInfoService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * 车辆信息控制器
 *
 * @author fengshuonan
 * @Date 2018-08-24 13:32:37
 */
@Controller
@RequestMapping("/carInfo")
public class CarInfoController extends BaseController {

    private String PREFIX = "/info/carInfo/";
    @Autowired
    private ICarInfoService carInfoService;
    @Autowired
    private ICarMaService carMaService;

    /**
     * 跳转到车辆信息首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "carInfo.html";
    }

    /**
     * 跳转到添加车辆信息
     */
    @RequestMapping("/carInfo_add")
    public String carInfoAdd() {
        return PREFIX + "carInfo_add.html";
    }

    /**
     * 跳转到修改车辆信息
     */
    @RequestMapping("/carInfo_update/{carInfoId}")
    public String carInfoUpdate(@PathVariable Integer carInfoId, Model model) {
        CarInfo carInfo = carInfoService.selectById(carInfoId);
        model.addAttribute("item",carInfo);
        LogObjectHolder.me().set(carInfo);
        return PREFIX + "carInfo_edit.html";
    }

    /**
     * 跳转到车辆信息详情
     */
    @RequestMapping("/carInfo_info/{rowId}")
    public String carInfoDetail(@PathVariable Integer rowId, Model model) {
        System.out.print(rowId);
        CarInfo carInfo = carInfoService.selectById(rowId);
        model.addAttribute("item",carInfo);
        LogObjectHolder.me().set(carInfo);
        return PREFIX + "carInfo_info.html";
    }

    /**
     * 下载车辆信息
     */
    @RequestMapping(value = "/download")
    @ResponseBody
    public void download( HttpServletResponse response,CarInfo info) throws IOException {

            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet("车辆信息表");

            List<CarInfo> listCar = carInfoService.getCarInfo(info.getPlateNumber(),info.getType(),info.getStatus());

            String fileName = "文件名名名"  + ".xls";//设置要导出的文件的名字
            //新增数据行，并且设置单元格数据

            int rowNum = 1;

            String[] headers = { "编号", "车牌号", "车类型", "车辆状态","车辆描述"};//headers表示excel表中第一行的表头

            HSSFRow row = sheet.createRow(0);//在excel表中添加表头

            for(int i=0;i<headers.length;i++){
                HSSFCell cell = row.createCell(i);
                HSSFRichTextString text = new HSSFRichTextString(headers[i]);
                cell.setCellValue(text);
            }

            //在表中存放查询到的数据放入对应的列
            for (CarInfo carInfo : listCar) {
                HSSFRow row1 = sheet.createRow(rowNum);
                row1.createCell(0).setCellValue(carInfo.getId());
                row1.createCell(1).setCellValue(carInfo.getPlateNumber());
                row1.createCell(2).setCellValue(carInfo.getType());
                row1.createCell(3).setCellValue(carInfo.getStatusView());
                row1.createCell(4).setCellValue(carInfo.getDescription());
                rowNum++;
            }

            response.setContentType("application/octet-stream");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            response.flushBuffer();
            System.out.println("测试导出excel");
            workbook.write(response.getOutputStream());
        }



    @RequestMapping(value = "/list")
    @ResponseBody
//    public Object list(@RequestParam(required = false) String plateNumber,@RequestParam(required = false) String type,@RequestParam(required = false) Integer status) {
        public Object list(String plateNumber,String type,Integer status) {
        List<CarInfo> listCar = carInfoService.getCarInfo(plateNumber, type, status);
        return listCar;
    }
    /**
     * 新增车辆信息
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(CarInfo carInfo) {
        carInfoService.insert(carInfo);
        CarMa carMa = new CarMa();
        carMa.setOrTime("");
        carMa.setPlateNumbr(carInfo.getPlateNumber());
        if (carInfo.getStatus()==1){
            carMa.setStatus(0);
        }else if (carInfo.getStatus()==0);
        carMa.setStatus(1);

        carMaService.insert(carMa);
        return SUCCESS_TIP;
    }

    /**
     * 删除车辆信息
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer carInfoId) {
        carInfoService.deleteById(carInfoId);
        carMaService.deleteById(carInfoId);
        return SUCCESS_TIP;
    }

    /**
     * 修改车辆信息
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(CarInfo carInfo) {
        carInfoService.updateById(carInfo);
        CarMa carMa = new CarMa();
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = dateFormat.format(date);
        carMa.setOrTime(strDate);
        carMa.setPlateNumbr(carInfo.getPlateNumber());
        carMa.setStatus(carInfo.getStatus());
        carMaService.updateById(carMa);
        return SUCCESS_TIP;
    }

    /**
     * 车辆信息详情
     */
    @RequestMapping(value = "/detail/{carInfoId}")
    @ResponseBody
    public Object detail(@PathVariable("carInfoId") Integer carInfoId) {
        return carInfoService.selectById(carInfoId);
    }
}
